package com.origin.app

import android.os.Bundle
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


class JsInterface {
    // Android 调用 Js 方法1 中的返回值
    @JavascriptInterface
    fun callAndroidBack() {
        Log.e("js","调用成功")
    }
}

class WebViewPage : AppCompatActivity() {
    lateinit var webView: WebView
    var webUrl:String="http://192.168.1.13:5173/#/x8_cloud"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web_view_page)
        webView=findViewById(R.id.webView)

        initWebView();
    }

     fun  initWebView(){

         val settings = webView.settings
         settings.domStorageEnabled = true

         //解决一些图片加载问题
         settings.javaScriptEnabled = true
         settings.blockNetworkImage = false
         // js 方法 window.android.callAndroidBack()
         webView.addJavascriptInterface(JsInterface(),"android")
         webView.webViewClient = object : WebViewClient() {
             override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                 Log.d("webview", "url: $url")
                 view.loadUrl(url)
                 return true
             }
         }
         webView.loadUrl(webUrl);
    }
}